//
//  LoginApi.m
//  EMMApp
//
//  Created by Mac on 16/5/24.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "LoginApi.h"
#define myUrlMethod @"login"
#define myUrlPath @"/appapi/login"

@implementation LoginApi
{
    NSString *_body;
}
#pragma mark - 初始化

- (instancetype)initWith:(NSString *)loginName
                loginPwd:(NSString *)loginPwd
{
    self = [super init];
    if (self) {
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        [params setValue:apiVersion forKey:@"version"];//版本号
        [params setValue:[IDTools devId2] forKey:@"terminalCode"];//设备号 终端编码
        [params setValue:loginName forKey:@"loginName"];//登陆帐号
        [params setValue:loginPwd forKey:@"loginPwd"];//登陆密码
        [params setValue:[IDTools devId] forKey:@"UUID"];//UUID
        [params setValue:merchantCode forKey:@"merchantCode"];//商户编码
        [params setValue:[IDTools idString2] forKey:@"deviceToken"];//极光注册码
        _body = [@{@"params" : params} JSONString];
        NSLog(@"---%@:\n %@%@?body=%@", myUrlMethod, apiBaseUrl, myUrlPath, _body);
        
    }
    return self;
}


#pragma mark - 父类继承拓展


//域名后面的网址信息 ————————————
- (NSString *)requestUrl
{
    return myUrlPath;
}

//需要传入的参数 ——————————————
- (id)requestArgument
{
    return @{ @"body" : _body };
}

/*返回数据请求的方式————————————————此处还应填此参数 数据请求方式，父类有，所以不用写
- (YTKRequestMethod)requestMethod
{
    return YTKRequestMethodPost;
}
 */

//______________________


- (NSString *)methodString
{
    return myUrlMethod;
}


#pragma mark - 通用

//将获取的数据直接填充到model里面 获得model
- (Login *)resultData
{
    id myObj = [IDTools jsonStrToObjc:self.responseString method:myUrlMethod];
    
    Login *data = (Login *)myObj;
    
    NSLog(@"%@ resultData is %@", myUrlMethod, data);
    
    return data;
}

- (BOOL)isSuccess
{
    // 得到model里面返回码 1代表成功
    NSString *str = [[self resultData] result];
    if ([NSString isStringEmpty:str] || ![str isEqualToString:@"1"]) {
        return NO;
    } else {
        return YES;
    }
}

@end
